package com.example.demo.controller;
import com.example.demo.APIS.DApi;
import com.alibaba.fastjson.JSON;
import com.example.demo.dao.productRepository;
import com.example.demo.model.WeichuListResponse;
import com.example.demo.model.productTable;
import com.example.demo.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by zouwenchang on 2018/1/26.
 */
@RestController
public class productController {

    @Autowired
    productRepository productRepository;



    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @GetMapping(value = "/productList")
    private String productList() {
        WeichuListResponse weichuRsp=new WeichuListResponse();

        weichuRsp.setMsg("success");
        weichuRsp.setCode("0000");
        weichuRsp.setItems((ArrayList) productRepository.findAll());

        return JSON.toJSONString(weichuRsp);
    }

    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @PostMapping(value = "/addProduct")
    private productTable addProduct(@RequestParam("file") MultipartFile file,
                                    @RequestParam("Id") Integer Id,
                                    HttpServletRequest request, HttpServletResponse response) throws IOException {
        productTable pros=new productTable();

        MultipartHttpServletRequest params = ((MultipartHttpServletRequest) request);
        String fileName = file.getOriginalFilename();


        try {
            FileUtil.saveFile( fileName,file.getBytes());

        } catch (Exception e) {
        }




        String urls= FileUtil.getURL(fileName);

        pros.setProImg(DApi.hostName+"/getImage?file="+fileName);
        pros.setId(Id);
        pros.setProTit(params.getParameter("proTit"));
        pros.setProDesc(params.getParameter("proDesc"));
        pros.setProTypes(params.getParameter("proTypes"));
        pros.setProStar(params.getParameter("proStar"));

        //返回json

        return productRepository.save(pros);
    }
    /**
     * 删除一个人员
     *
     * @param id
     */
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @PostMapping(value = "/deleteProduct/{id}")
    public void personDelete(@PathVariable("id") Integer id) {

        productRepository.delete(id);
    }

    /**
     * 更新一个产品
     *
     * @param id
     * @param file
     * @param proTit
     * @param proDesc
     * @param proTypes
     * @param proStar
     * @return
     */
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @PostMapping(value = "/editProduct/{id}")
    private productTable editProduct(@RequestParam("file") MultipartFile file,
                                    @PathVariable("id") Integer Id,
                                    HttpServletRequest request, HttpServletResponse response) throws IOException {
        productTable prod = new productTable();

        MultipartHttpServletRequest params=((MultipartHttpServletRequest) request);
        String fileName = file.getOriginalFilename();


        try {
            FileUtil.saveFile( fileName,file.getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        }


        String urls= FileUtil.getURL(fileName);

        prod.setProImg(DApi.hostName+"/getImage?file="+fileName);
        prod.setId(Id);
        prod.setProTit(params.getParameter("proTit"));
        prod.setProDesc(params.getParameter("proDesc"));
        prod.setProTypes(params.getParameter("proTypes"));
        prod.setProStar(params.getParameter("proStar"));


        return productRepository.save(prod);
    }


}
